<?php
include('../db_connect.php');

$add_course=true;

if($_POST["courseDepartment"] == ""){
	$add_course=false;
    echo "Sorry, could not validate course department.";
    return;
}
$str_length=strlen($_POST['courseDepartment']);
if($str_length<2 || $str_length >6){
    echo "Sorry, could not validate course department.";
    return;
}

if($_POST["courseNumber"] == "" || !is_numeric($_POST['courseNumber'])){
	$add_course=false;
    echo "Sorry, could not validate course number.";
    return;
}
$str_length=strlen($_POST['courseNumber']);
if($str_length<2 || $str_length >4){
    echo "Sorry, could not validate course number.";
    return;
}
if($_POST["courseName"] == ""){
	$add_course=false;
    echo "Sorry, could not validate course name.";
    return;
}
$str_length=strlen($_POST['courseName']);
if($str_length<2 || $str_length >30){
    echo "Sorry, could not validate course name.";
    return;
}

$courseDepartment = $_POST['courseDepartment'];
$courseNumber = $_POST['courseNumber'];
$courseName = $_POST['courseName'];
$i_courseNumber = (int) $courseNumber;

//make sure this course doesn't exist
$sql="select id from fb_courses where course_dept='$courseDepartment' and course_num=$i_courseNumber";
$result = $db->Execute($sql);
$course_id = $result->fields['id'];
if($course_id)
{
    $add_course=false;
    echo "Sorry, this course has already been added.";
    return;
}

//we are validated, add course to database 
if($add_course){ 
	$sql="insert into fb_courses values (DEFAULT,'16777600','$courseDepartment','$courseNumber','$courseName')";
	$db->Execute($sql);

	//get the course id
    $sql="select id from fb_courses where network_id=16777600 and course_dept='$courseDepartment' and course_num=$i_courseNumber";
    $result = $db->Execute($sql);
    $course_id = $result->fields['id'];

    echo "Great success! Added course $courseDepartment $courseNumber $courseName";

    //make document directory structure
    mkdir("../w2box/data/$courseDepartment $courseNumber $courseName", 0700);

    //add course forum category
    $sql = "select COUNT(*) from mlf2_categories";
    $result = $db->Execute($sql);
    list($category_count) = $result->fields[0];
    $category_count = $category_count +1;
    $sql = "insert into mlf2_categories values ('$category_count','$category_count','$courseDepartment $courseNumber','',0,'$course_id')";
    $db->Execute($sql); 

    //auto create upcoming chat forum
    $sql="insert into mlf2_entries (pid,tid,uniqid,time,last_reply,edited,name,subject,category,email,hp,location,ip,text,tags,sticky,edit_key) values('0','0','aaaaa',NOW(),NOW(),NOW(),'REACH','".$courseDepartment." ".$courseNumber." Tutor Sessions','".$category_count."','','','','','This thread is designated for REACH tutors to post times they will be available for discussion in the ".$courseDepartment." ".$courseNumber." channel, or times for tutoring on campus.','','1','')";
    $db->Execute($sql); 
    $sql="update mlf2_entries set tid=id,time=time where id = LAST_INSERT_id()";
    $db->Execute($sql); 

}
?>
